package view;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import battleModel.CampaignModel;
import battleModel.Tank;

/**
 * 
 * @author Kyle Bush
 *
 */
@SuppressWarnings("serial")
public class LanView extends MasterViewPanel {

	private Tank player;
	private String currColor, currMap;
	
	private Image img;
	
	private JPanel levelPanel, optionsPanel, body;
	private GameButton[] levels;
	private JRadioButton[] colors;
	private JTextField playerName, host;
	private JLabel tank; 
	private boolean levelSelected;	
	private float alpha;
	
	
	
	private final int COLORS = 8, LEVELS = 20;	
	private String background = "images/backgrounds/lanMenu.png";


	public LanView(MasterView m) {
		super(m);
		int i = CampaignModel.LEVELS;
		player = new Tank();
		alpha = 0.2f;
		setUpLayout();
		setUpPanels();
	}

	private void setUpLayout() {
		this.setLayout(null);
	}

	private void setUpPanels() {
		setUpLevelPanel();
		setUpOptionsPanel();
		setUpTraverseButtons();
		drawBackGround();
	}

	/*
	 * Set up level button panel
	 */
	private void setUpLevelPanel() {
		body = new JPanel(new BorderLayout());
		body.setOpaque(false);
		body.setSize(450, 500);
		body.setLocation(20, 75);
		body.setOpaque(false);
		body.setVisible(true);
		
		
		levelPanel = new JPanel(new GridLayout(10, 2, 7, 7));
		levelPanel.setSize(450, 500);
		levelPanel.setLocation(20, 75);
		levelPanel.setOpaque(false);
		levelPanel.setVisible(true);
		
		body.add(levelPanel);
		this.add(body);

		setUpMapButtons();
	}

	/*
	 * Set up player options panel
	 */
	private void setUpOptionsPanel() {
		optionsPanel = new JPanel(new GridLayout(4, 0, 2, 2));
		optionsPanel.setSize(250, 335);
		optionsPanel.setLocation(550, 50);
		optionsPanel.setOpaque(false);
		optionsPanel.setVisible(true);
		this.add(optionsPanel);

		setUpTextFields();
		setUpColorOptions();
		setUpDisplayTank();
	}

	/*
	 * Set up level buttons
	 */
	private void setUpMapButtons() {
		String text = "";
		levels = new GameButton[CampaignModel.LEVELS];

		for (int i = 0; i < CampaignModel.LEVELS; i++) {
			text = "MAP " + (i + 1);			
			levels[i] = new GameButton(m.getIcon("levelButton"), m.getIcon("levelButtonS"));
			levels[i].setSelectedIcon(m.getIcon("levelButtonsS"));
			levels[i].setDisabledIcon(m.getIcon("lockedLevel"));
			levels[i].addActionListener(new LevelSelectListener());
			levels[i].setActionCommand("" + (i + 1));
			levels[i].setText(text);
			
			
			levelPanel.add(levels[i]);
			text = null;
		}
		
		GameButton customMap = new GameButton(m.getIcon("customLevel"), m.getIcon("customLevelS"));
		customMap.setText("Custom Map");
		body.add(customMap, BorderLayout.SOUTH);
		
		levelSelected = false;
	}

	/*
	 * Set up text fields
	 */
	private void setUpTextFields() {
		JPanel filler = new JPanel();
		filler.setOpaque(false);

		JPanel namePanel = new JPanel(new GridLayout(2, 0));
		namePanel.setOpaque(false);

		JLabel header = new JLabel("ENTER YOUR NAME");
		header.setForeground(Color.WHITE);
		header.setFont(new Font("Stencil", Font.PLAIN, 25));

		playerName = new JTextField("<Name>", 25);
		playerName.setOpaque(false);
		playerName.setForeground(Color.WHITE);
		playerName.setBackground(new Color(50, 50, 50, 200));
		
		namePanel.add(header);
		namePanel.add(playerName);
		
		JPanel hostPanel = new JPanel(new GridLayout(2, 0));
		hostPanel.setOpaque(false);
		
		header = new JLabel("ENTER HOST IP");
		header.setForeground(Color.WHITE);
		header.setFont(new Font("Stencil", Font.PLAIN, 25));

		host = new JTextField("<IP>", 25);
		host.setOpaque(false);
		host.setForeground(Color.WHITE);
		host.setBackground(new Color(50, 50, 50, 200));
		
		hostPanel.add(header);
		hostPanel.add(host);

		optionsPanel.add(namePanel);
		optionsPanel.add(hostPanel);
	}

	/*
	 * Set up color selection buttons
	 */
	private void setUpColorOptions() {
		JPanel colorPanel = new JPanel(new GridLayout(2, 0));
		JPanel btnPanel = new JPanel(new GridLayout(2, 4));
		
		//Header of color selector
		JLabel header = new JLabel("SELECT A COLOR");
		header.setFont(new Font("Stencil", Font.PLAIN, 25));
		header.setForeground(Color.WHITE);
		
		//Color buttons
		colors = new JRadioButton[COLORS];
		String[] color = { "RD", "OR", "YL", "GN", "BL", "VT", "BK", "WT" };
		
		//Color button Images
		BufferedImage colorBtn = null, colorBtnS = null;
		try {
			colorBtn = ImageIO.read(new File("images/buttons/colors.png"));
			colorBtnS = ImageIO.read(new File("images/buttons/colorsS.png"));
		} catch (IOException e) {}

		//Set up buttons with images
		Image img1 = null, img2 = null;
		for (int i = 0; i < COLORS; i++) {
			img1 = colorBtn.getSubimage((i * 20), 0, 20, 20);
			img2 = colorBtnS.getSubimage((i * 20), 0, 20, 20);
			ImageIcon icon = new ImageIcon(img1);
			ImageIcon iconS = new ImageIcon(img2);

			colors[i] = new JRadioButton(icon);
			colors[i].setRolloverIcon(iconS);
			colors[i].setSelectedIcon(iconS);
			colors[i].setActionCommand(color[i]);
			colors[i].addActionListener(new ColorListener());

			//Default color "RED" selected
			if (i == 0) {
				colors[i].setSelected(true);
				currColor = "RD";
			}

			btnPanel.setOpaque(false);
			btnPanel.add(colors[i]);
		}

		colorPanel.add(header);
		colorPanel.add(btnPanel);
		colorPanel.setOpaque(false);
		
		optionsPanel.add(colorPanel);
	}

	/*
	 * Set up traverse buttons (Host, Join, Back)
	 */
	private void setUpTraverseButtons() {
		GameButton join = new GameButton(m.getIcon("join"), m.getIcon("joinS"));
		GameButton host = new GameButton(m.getIcon("host"), m.getIcon("hostS"));
		GameButton menu = new GameButton(m.getIcon("mainMenu"), m.getIcon("mainMenuS"));

		host.addActionListener(new HostGameListener());
		join.addActionListener(new JoinGameListener());
		menu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				m.changeView(Views.MENU, null);
			}
		});

		host.setSize(300, 50);
		host.setLocation(530, 410);
		join.setSize(300, 50);
		join.setLocation(530, 480);
		menu.setSize(300, 50);
		menu.setLocation(530, 550);

		this.add(host);
		this.add(join);
		this.add(menu);
	}
	
	/*
	 * Sets up the display of tank color
	 */
	private void setUpDisplayTank() {
		tank = new JLabel(new ImageIcon("images/tankDisplay/" 
				+ currColor + ".png"));
		optionsPanel.add(tank);
	}

	/*
	 * A method that updates the tank color display
	 */
	private void updateTank() {
		tank.setIcon(new ImageIcon("images/tankDisplay/" + currColor + ".png"));
	}
	
	private void drawBackGround() {
		try {
			img = ImageIO.read(new File(background));
		} catch (IOException e) {
			e.printStackTrace();
		}
		repaint();
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);Graphics2D g2d = (Graphics2D) g;

		// set the opacity
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,alpha));
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

		g.drawImage(img, 0, 0, this);

		// increase the opacity and repaint
		alpha += 0.03f;
		if (alpha >= 1.0f) {
			alpha = 1.0f;
		} else {
			repaint();
		}

		// sleep for a bit
		try {
			Thread.sleep(5);
		} catch (InterruptedException e) {

			e.printStackTrace();
		}
	}
	
	private class HostGameListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			//IMPLEMENT
			JOptionPane.showMessageDialog(null, "Hosting Game");
		}
	}
	
	private class JoinGameListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			//IMPLEMENT 
			JOptionPane.showMessageDialog(null, "Join Game");
		}
	}
	
	private class LevelSelectListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {

			//Level Selection
			GameButton temp = levels[Integer.valueOf(e.getActionCommand()) - 1];

			//If level selected is already selected, set to unselected
			if (temp.levelSelected()) {
				temp.setIcon(m.getIcon("levelButton"));
				temp.setLevelSelected(false);
				currMap = "";
				levelSelected = false;
			} else { 
				//If some level is selected
				if(levelSelected){ 
					//Find level selected, and unselect
					for (int i = 0; i < LEVELS; i++) {
						if (levels[i].levelSelected()) {
							levels[i].setLevelSelected(false);
							levels[i].setIcon(m.getIcon("levelButton"));
						}
					}
				}
				
				//Set level selected as selected
				temp.setIcon(m.getIcon("levelButtonS"));
				temp.setLevelSelected(true);
				currMap = e.getActionCommand();
				levelSelected = true;
			}
		}
	}

	private class ColorListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			
			//Color selected is different then that already selected
			if (!currColor.equals(e.getActionCommand())) {
				for (int i = 0; i < COLORS; i++) {
					//Find selected color and unselect
					if (colors[i].getActionCommand().equals(currColor))
						colors[i].setSelected(false);
				}
				
				//Set current color, update tank display
				currColor = e.getActionCommand();
				updateTank();
			}else{
				for (int i = 0; i < COLORS; i++) {
					if (colors[i].getActionCommand().equals(currColor))
						colors[i].setSelected(true);
				}
			}	
		}
	}
	
	/**
	 * String representation of panel name.
	 */
	public String toString() {
		return "LAN";
	}
}
